"""
关联表模型
"""
from datetime import datetime
from app.extensions import db


class CustomerStaff(db.Model):
    """客户-员工关联表 - 支持多对多关系"""
    id = db.Column(db.Integer, primary_key=True)
    customer_id = db.Column(db.Integer, db.ForeignKey('customer.id'), nullable=False)
    staff_id = db.Column(db.Integer, db.ForeignKey('staff.id'), nullable=False)
    project_name = db.Column(db.String(100), comment='合作项目名称')
    is_active = db.Column(db.Boolean, default=True, comment='是否有效')
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    
    # 为兼容性添加的字段
    relationship_type = db.Column(db.String(50), comment='关系类型')
    
    # 关联
    customer = db.relationship('Customer', backref=db.backref('customer_staffs', lazy=True))
    staff = db.relationship('Staff', backref=db.backref('customer_staffs', lazy=True))
    
    def __repr__(self):
        return f'<CustomerStaff {self.customer_id}-{self.staff_id}>'
